<?php
/**
 * @author guoxiaoyong
 * 社团
 */
namespace frontend\controllers;

use bc\controller\FrontController;
use yii\web\Session;
use backend\models\organization\OrganizationModel;
use yii\helpers\ArrayHelper;
use backend\models\organization\ApplyModel;
use backend\models\organization\OrganizationUserModel;
use backend\models\organization\ActivityModel;
use bc\ar\MActiveRecord;

class OrganizationController extends FrontController
{
	
	public function actionIndex()
	{
		$this->setOAuth();
		
		$session = new Session();
		$user = $session->get('user');
		
		
		if(!$user)
			$this->redirect('/', 404);
		
		$dao = new MActiveRecord();
			
		/*
		$list = [];
		
		
		//已加入
		$joinSql = 'select * from bc_organization_user as `user` left join bc_organization as o on `user`.organization_id = o.id where user.`status` = 1 and `user`.user_id = '.$user['id'] . ' limit 3';
		$join = $dao->getDb()->createCommand($joinSql)->queryAll();
		
		
		if($join)
		{
			foreach ($join as $item)
			{
				$item['join_info'] = '已入社';
				$item['class'] = 'joined';
				$list[] = $item;
			}
		}
		
		//申请中
		$applySql = 'select * from bc_organization_apply as apply left join bc_organization as o on apply.organization_id = o.id where apply.`status` = 1 and apply.user_id = '.$user['id'] .' limit 3';
		$apply =  $dao->getDb()->createCommand($applySql)->queryAll();
		if($apply)
		{
			foreach ($apply as $item)
			{
				$item['join_info'] = '已申请';
				$item['class'] = 'waiting';
				$list[] = $item;
			}
		}
		*/
		
		$joinSql = 'select * from bc_classroom where status = 1 order by update_time desc limit 3';
		$list = $dao->getDb()->createCommand($joinSql)->queryAll();
		
		$bannerSql = 'select * from bc_banner where status = 1 order by t_sort limit 5';
		$banner_list = $dao->getDb()->createCommand($bannerSql)->queryAll();
		
		$view['list'] = $list;
		
		$view['banner_list'] = $banner_list;
		
		return $this->render('index', $view);
	}
	
	public function actionList()
	{
		$session = new Session();
		$user = $session->get('user');
		
		if(!$user)
			$this->redirect('/', 404);
		
// 		$user['id'] = 3;
		
		$view = [];
		if($user)
		{
			$model = new OrganizationModel;
			//已加入
			$organizationSql = 'select * from bc_organization where status = 1';
			$organization = OrganizationModel::findBySql($organizationSql)->all();
			
			$organization = ArrayHelper::toArray($organization);
			
			
			foreach ($organization as $key=>$item)
			{
				//已加入
				$joinSql = 'select user_id from bc_organization_user where user_id = ' . $user['id'] . ' AND organization_id = ' . $item['id'];
				$join = OrganizationModel::findBySql($joinSql)->one();
				
				if($join)
				{
					$organization[$key]['user_status'] = 1;
					$organization[$key]['user_status_name'] = '已加入';
					$organization[$key]['class'] = 'btn-join';
					continue;
				}
					
				//已申请
				$applySql = 'select user_id from bc_organization_apply where user_id = ' . $user['id']  . ' AND organization_id = ' . $item['id'];
				$apply = OrganizationModel::findBySql($applySql)->one();
				if($join)
				{
					$organization[$key]['user_status'] = 2;
					$organization[$key]['user_status_name'] = '已申请';
					$organization[$key]['class'] = 'btn-join';
					continue;
				}
				
				//未加入
				$organization[$key]['user_status'] = 3;
				$organization[$key]['user_status_name'] = '申请入社';
				$organization[$key]['class'] = 'btn-join new';
			}
			
			
			$view['organization'] = $organization;
			
		}
		return $this->render('list', $view);
	}
	
	public function actionJoin()
	{
		$view = [];
		return $this->render('join', $view);
	}
	
	
	/**
	 * 保存我要推广的页面
	 * 
	 */
	public function actionJoinsave()
	{
		if($this->getRequest()->isPost == true)
		{
			$session = new Session();
			$userSession = $session->get('user');
			if(!$userSession)
			{
				$this->redirect('/', 404);
			}
			
			$data['organization_id'] = $this->post('id');
			$data['interest'] = $this->post('interest');
			$data['open_id'] = $userSession['openid'];
			$data['user_status'] = $this->post('user_status');
			$data['apply_description'] = $this->post('description');
			$data['user_id'] = $userSession['id'];
			$data['create_time'] = time();
			
			
			if(organization_id)
			{
				$applyModel = new ApplyModel();
				
				$row = $applyModel->getRow(['organization_id' => $data['organization_id'], /*'status' => 1,*/ 'open_id' => $data['open_id']]);
				
				
				$current_time = time();
				
				if(!$row)
				{
					$applyModel->add($data);
					$msg = '申请成功';
					$this->Toast('申请成功');
				}
				else 
				{
					//申请时间超过48小时
					if( ($current_time -$row['create_time'] ) > 172800)
					{
						$applyModel->add($data);
						$msg = '申请成功';
						$this->Toast('申请成功');
					}
					else 
					{
						$this->Toast('已申请、请勿重复申请');
						$msg = '已申请、请勿重复申请';
					}
					
				}
				
				
				echo "<script>alert('{$msg}');window.location.href='/organization'</script>";
				
			}
			else 
			{
				$this->redirect('/', 404);
			}
		}
		else 
		{
			$this->redirect('/', 404);
		}
		
	}
	
	/**
	 * 社团准则
	 */
	public function actionNotice()
	{
		return $this->render('notice');
	}
	
	
	/**
	 * 新手指南
	 */
	public function actionGuide()
	{
		return $this->render('guide');
	}
	
	public function actionDetail()
	{
		$icon = [];		
		$sessionUser = new Session();
		$user = $sessionUser->get('user');
		
// 		$user['id'] = 3;
		
		$id = $this->get('id');
		if(!$id || !$user)
			$this->redirect('/', 404);
		
		//2:简介  3：活动
		$type = $this->get('type', 1);
		
		$OrganizaionModel = new OrganizationModel();
		$organization = $OrganizaionModel->getRow(['id' => $id, 'status' => 1]);
		
		if(!$organization)
			$this->redirect('/', 404);
		$OrganizaionActivityModel = new ActivityModel();
		$OrganizaionActivityModel->sort = 'create_time DESC';
		$activityList = $OrganizaionActivityModel->getList(['organization_id' => $id, 'status' => 1]);
		
		
		$OrganizaionUserModel = new OrganizationUserModel();
		$OrganizaionUser = $OrganizaionUserModel->getRow(['user_id' => $user['id'], 'organization_id' => $organization['id'], 'user_id_status' => 1]);
		if($OrganizaionUser)
		{
			$icon['class'] = '';
			$icon['name'] = '已入社';
			$icon['status'] = 1;
		}
		else 
		{
			$ApplyModle = new ApplyModel();
			$Apply = $ApplyModle->getRow(['user_id' => $user['id'], 'organization_id' => $id, 'status' => 1]);
			if($Apply)
			{
				$icon['class'] = '';
				$icon['name'] = '已申请';
				$icon['status'] = 2;
			}
			else 
			{
				$icon['class'] = 'new';
				$icon['name'] = '申请入社';
				$icon['status'] = 3;
			}
		}
		
		
		$view['organization'] = $organization;
		$view['activityList'] = $activityList;
		$view['icon'] = $icon;
		$view['type'] = $type;
		
		return $this->render('detail', $view);
	}
	
	
	/**
	 * 
	 */
	public function actionTimetable()
	{
		return $this->render('timetable');
	}
}

